﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using RoadTeam.Model;
using RoadTeam.Enums;
using RoadTeam.SysInterface;
using RoadTeam.Pub;

namespace RoadTeam.BLL
{
    
    public class CsValidateObjectBll
    {
        private static object o = new object();
        public DataTable GetValidateObjectData(String OrganIdentifier)
        {
            ReflashData(OrganIdentifier);
            SqlWhere where = new SqlWhere();
            where.Info = new CsValidateObject() { OrganIdentifier = OrganIdentifier };
            //where.SelectColumn = @" ValidateObjectIdentifier,ValidateObjectName,ValidateObjectCode,MaintenanceTime ";
            return CsValidateObject.Select(where, GlobalConfig.ConnectionString);
        }

        private void ReflashData(string OrganIdentifier)
        {
            String OrganID = FactoryClass.CreateSmOrgan().GetOraganID(OrganIdentifier);
            DataTable dt_object = GetEmptyTable();
            DataRow dr_object;
            lock (o)
            {
                DataTable dt = FactoryClass.CreateCsValidateObject().GetValidateObjectTable(OrganID);
                foreach (DataRow dr in dt.Rows)
                {
                    dr_object = dt_object.NewRow();
                    dr[3] = CreateIdentifier.GetIdentifier();
                    dr_object["ValidateObjectIdentifier"] = dr[3];
                    dr_object["ValidateObjectName"] = dr[0];
                    dr_object["ValidateObjectCode"] = dr[1];
                    dr_object["MaintenanceTime"] = dr[2];
                    dr_object["OrganIdentifier"] = FactoryClass.CreateSmOrgan().GetIdentitiferByID(dr[4].ToString());
                    dt_object.Rows.Add(dr_object);
                }
                FactoryClass.CreateCsValidateObject().UpdateValidateObjectTable(dt);
            }
            CsValidateObject.InsertDatatable(dt_object, GlobalConfig.ConnectionString);
        }


        public DataTable GetEmptyTable()
        {
            SqlWhere where = new SqlWhere();
            where.Info = new CsValidateObject() { ValidateObjectIdentifier=@"-1" };
            where.SelectColumn = @" ValidateObjectIdentifier,ValidateObjectName,ValidateObjectCode,MaintenanceTime,OrganIdentifier ";
            return CsValidateObject.Select(where, GlobalConfig.ConnectionString);
        }

    }
}
